﻿using api.Plugins;
using DataProvider;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json;
using Utility;

namespace api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class Sys_RoleController : ControllerBase
    {
        private readonly Sys_RoleProvider _RoleProvider= Sys_RoleProvider.Instances;
        public Sys_RoleController()
        {
        }
       
        [HttpPost]
        public IActionResult GetList(RoleQuery param)
        {
            var res = _RoleProvider.GetList(param, out int total);
            return Ok(JsonConvert.SerializeObject(new ResultData<object>
            {
                data = res,
                count = total
            }));
        }
        [HttpPost]
        public IActionResult SaveForm(Sys_Role param)
        {
            if (param == null || string.IsNullOrEmpty(param.RoleName))
                return Ok(ResultData.TipMsg("Parameter exception！"));

            var user = this.GetUserToken();
            var res = _RoleProvider.SaveModel(user.UserId, param,out string errMsg);
            return Ok(res>0?ResultData.Success("Saved successfully！") :ResultData.TipMsg(errMsg));
        }
        [HttpPost]
        public IActionResult Delete(int[] ids)
        {
            if (ids == null || ids.Length == 0)
                return Ok(ResultData.TipMsg("Parameter exception ！"));

            var userToken = this.GetUserToken();
            var res = _RoleProvider.Del(userToken.UserId,ids);
            return Ok(res > 0 ? ResultData.Success("Deleted successfully！") : ResultData.TipMsg("Delete failed！"));
        }

        /*生成菜单ztree，给角色分配菜单权限用。
{
    "code": 200,
    "msg": "",
    "data": [
        {
            "name": "系统管理 ",
            "checked": true,
            "pId": -1,
            "id": 1,
            "open": true
        }
    ]
}         */
    }
}
